import requests
import asyncio


async def download_pics(session, url):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.3'}
    print("开始下载： ", url)
    # 使用线程池完成异步下载
    loop = asyncio.get_event_loop()
    future = loop.run_in_executor(None, requests.get, url)

    response = await future
    print('下载完成')
    # 图片保存到本地文件
    file_name = url.rsplit('_')[-1]
    with open(file_name, 'wb') as fp:
        fp.write(response.content)
    print('下载完成: ', url)


async def main():
    url_list = {
        'https://car2.autoimg.cn/cardfs/product/g25/M0B/99/EF/1024x0_1_q95_autohomecar__ChwFj19_uIWAQQgZABaZxAqb8BU552.jpg',
        'https://car3.autoimg.cn/cardfs/product/g25/M0A/5E/53/1024x0_1_q95_autohomecar__ChsEmF9zQUuAOt79ACT7Rgx018s791.jpg',
        'https://car2.autoimg.cn/cardfs/product/g26/M01/5B/D4/1024x0_1_q95_autohomecar__ChwFkF9zQc6AesUsACav7Ndb9c4212.jpg'
    }
    tasks = [
        download_pics(url) for url in url_list
    ]

    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(tasks))


if __name__ == '__main__':
    asyncio.run(main())
